/*INIT*/
$(document).ready(function() {
	formChecker.init();
});

var formChecker = {
	init: function() {
		$('#form_chgPassword [name=oldpassword]').focusout(formChecker.checkOldPassword).focusout();
		$('#form_chgPassword [name=newpassword]').focusout(formChecker.checkPassword);
		$('#form_chgPassword [name=renewpassword]').focusout(formChecker.checkRepassword);
		$('#form_chgPassword .btnBase').click(function(){
			var oldpass = $('#form_chgPassword [name=oldpassword]').val();
			var newpass = $('#form_chgPassword [name=newpassword]').val();

			if($('#form_chgPassword .checker_ok').length == 3)
			{
				$.ajax({
			        url : getRootPath() + '/customerajax/changepassword!change.action',
			        data :{oldpassword:oldpass, newpassword:newpass},
			        cache : false, 
			        async : true,
			        type : "POST",
			        dataType : 'json',
			        beforeSend	: function(xmlHttpRequest){
						//开启提交动画
					},
			        success : function (data){
					    //关闭提交动画，响应成功请求
						if(null != data.errorCode && data.errorCode!=""){
							alert(data.errorCode);
							return false;
						}
						alert('密码修改成功!');
						$('#form_chgPassword').find('input').val("");
			        },
			        error	:	function(){
			        	//关闭提交动画，响应失败请求
			        	alert("密码修改失败，请重试！");
			        }
			    });
			}
				
			//self.location='index.jsp';
		});
	},
	checkOldPassword: function() {
		var pass = $('#form_chgPassword [name=oldpassword]').val();
		if(pass == '') {
			$('#form_chgPassword [name=checker_oldpassword]').removeClass().addClass('checker_notice');
			$('#form_chgPassword [name=checker_oldpassword] span').html('请输入您的原始密码');
			return false ;
		}
		else {
			$('#form_chgPassword [name=checker_oldpassword]').removeClass().addClass('checker_ok');
			$('#form_chgPassword [name=checker_oldpassword] span').html('');
			return true ;
		}
	},
	checkPassword: function() {
		var pass = $('#form_chgPassword [name=newpassword]').val();
		var repass = $('#form_chgPassword [name=renewpassword]').val();
		if(pass == '') {
			$('#form_chgPassword [name=checker_newpassword]').removeClass().addClass('checker_notice');
			$('#form_chgPassword [name=checker_newpassword] span').html('请输入您的新密码');
			return false ;
		}
		else if(pass.length < 6) {
			$('#form_chgPassword [name=checker_newpassword]').removeClass().addClass('checker_wrong');
			$('#form_chgPassword [name=checker_newpassword] span').html('密码至少为6位');
			return false ;
		}
		else if(repass != '' && repass != pass) {
			$('#form_chgPassword [name=checker_newpassword]').removeClass().addClass('checker_wrong');
			$('#form_chgPassword [name=checker_newpassword] span').html('您两次输入的新密码不同');
		}
		else {
			$('#form_chgPassword [name=checker_newpassword]').removeClass().addClass('checker_ok');
			$('#form_chgPassword [name=checker_newpassword] span').html('');
			return true ;
		}
	},
	checkRepassword: function() {
		var pass = $('#form_chgPassword [name=newpassword]').val();
		var repass = $('#form_chgPassword [name=renewpassword]').val();
		if(pass == '' && repass == '') {
			$('#form_chgPassword [name=checker_renewpassword]').removeClass();
			$('#form_chgPassword [name=checker_renewpassword] span').html('');
			return false ;
		}
		else if(pass != '' && repass == '') {
			$('#form_chgPassword [name=checker_renewpassword]').removeClass().addClass('checker_notice');
			$('#form_chgPassword [name=checker_renewpassword] span').html('请再次输入您的新密码');
			return false ;
		}
		else if(pass == repass) {
			$('#form_chgPassword [name=checker_renewpassword]').removeClass().addClass('checker_ok');
			$('#form_chgPassword [name=checker_renewpassword] span').html('');
			return true ;
		}
		else {
			$('#form_chgPassword [name=checker_renewpassword]').removeClass().addClass('checker_wrong');
			$('#form_chgPassword [name=checker_renewpassword] span').html('您两次输入的新密码不同');
			return false ;
		}
	}
};